package me.jiatao.kotlinmall.widget

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.navigation.NavigationView
import me.jiatao.baselibrary.ext.onClick
import me.jiatao.baselibrary.utils.Preference
import me.jiatao.kotlinmall.R
import me.jiatao.kotlinmall.isLogin
import me.jiatao.kotlinmall.ui.activity.MainActivity
import me.jiatao.kotlinmall.utils.SettingUtil
import me.jiatao.provider.common.ProviderConstant
import me.jiatao.provider.router.RouterDispatcher
import org.jetbrains.anko.toast

class MainNavigationView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : NavigationView(context, attrs, defStyleAttr) {


    /**
     * local username
     */
    private var username: String by Preference(ProviderConstant.USERNAME_KEY, "")

    /**
     * username TextView
     */
    private var nav_username: TextView? = null
    /**
     * user_id TextView
     */
    private var nav_user_id: TextView? = null
    /**
     * user_grade TextView
     */
    private var nav_user_grade: TextView? = null
    /**
     * user_rank TextView
     */
    private var nav_user_rank: TextView? = null
    /**
     * score TextView
     */
    private var nav_score: TextView? = null
    /**
     * rank ImageView
     */
    private var nav_rank: ImageView? = null


    init {
        nav_username = getHeaderView(0).findViewById(R.id.tv_username)
        nav_user_id = getHeaderView(0).findViewById(R.id.tv_user_id)
        nav_user_grade = getHeaderView(0).findViewById(R.id.tv_user_grade)
        nav_user_rank = getHeaderView(0).findViewById(R.id.tv_user_rank)
        nav_rank = getHeaderView(0).findViewById(R.id.iv_rank)
        nav_score = this.menu.findItem(R.id.nav_score).actionView as TextView
        nav_score?.gravity = Gravity.CENTER_VERTICAL

        menu.findItem(R.id.nav_logout).isVisible = true


        /**
         * 设置用户昵称
         */
        nav_username?.run {
            if (isLogin()) {
                text = username
            } else {
                text = context.getString(R.string.go_login)
                onClick {
                    context.toast("点击")
                    RouterDispatcher.UserCenter.navLogin()
                }
            }
        }



        setNavigationItemSelectedListener { item: MenuItem ->
            when (item.itemId) {
                R.id.nav_night_mode -> {
                    if (SettingUtil.getIsNightMode()) {
                        SettingUtil.setIsNightMode(false)
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                    } else {
                        SettingUtil.setIsNightMode(true)
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                    }
                    (context as MainActivity).window.setWindowAnimations(R.style.WindowAnimationFadeInOut)
                }
            }
            true
        }
    }
}